package it.polimi.traveldream.ejb;

import it.polimi.traveldream.dto.AccomodationDTO;
import it.polimi.traveldream.dto.ActivityDTO;
import it.polimi.traveldream.dto.TransportDTO;
import it.polimi.traveldream.entity.Accomodation;
import it.polimi.traveldream.entity.Activity;
import it.polimi.traveldream.entity.Transport;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class ProductManagerBean implements ProductManager {

	@PersistenceContext
	private EntityManager em;

	@Override
	public void addActivity(ActivityDTO activity) {
		Activity newActivity = new Activity(activity);
		this.em.persist(newActivity);
	}

	@Override
	public void addTransport(TransportDTO transport) {
		Transport newTransport = new Transport(transport);
		this.em.persist(newTransport);
	}

	@Override
	public void addAccomodation(AccomodationDTO accomodation) {
		Accomodation newAccomodation = new Accomodation(accomodation);
		this.em.persist(newAccomodation);
	}

	@Override
	public void removeActivity(ActivityDTO activity) {
		Activity entity = this.em.find(Activity.class, activity.getId());
		this.em.remove(entity);
	}

	@Override
	public void removeTransport(TransportDTO transport) {
		Transport entity = this.em.find(Transport.class, transport.getId());
		this.em.remove(entity);
	}

	@Override
	public void removeAccomodation(AccomodationDTO accomodation) {
		Accomodation entity = this.em.find(Accomodation.class, accomodation.getId());
		this.em.remove(entity);
	}

	@Override
	public List<ActivityDTO> getActivities() {
		List<ActivityDTO> list = new ArrayList<>();
		List<Activity> activities = this.em.createNamedQuery(Activity.FIND_ALL, Activity.class)
				.getResultList();
		for (Activity activity : activities) {
			list.add(activity.convertToDTO());
		}
		return list;
	}

	@Override
	public List<TransportDTO> getTransports() {
		List<TransportDTO> list = new ArrayList<>();
		List<Transport> transports = this.em.createNamedQuery(Transport.FIND_ALL, Transport.class)
				.getResultList();
		for (Transport transport : transports) {
			list.add(transport.convertToDTO());
		}
		return list;
	}

	@Override
	public List<AccomodationDTO> getAccomodations() {
		List<AccomodationDTO> list = new ArrayList<>();
		List<Accomodation> accomodations = this.em.createNamedQuery(Accomodation.FIND_ALL,
				Accomodation.class).getResultList();
		for (Accomodation accomodation : accomodations) {
			list.add(accomodation.convertToDTO());
		}
		return list;
	}

	@Override
	public void save(AccomodationDTO accomodation) {
		this.em.persist(new Accomodation(accomodation));
	}

	@Override
	public void save(TransportDTO transport) {
		this.em.persist(new Transport(transport));
	}

	@Override
	public void save(ActivityDTO activity) {
		this.em.persist(new Activity(activity));
	}

	@Override
	public List<AccomodationDTO> getAccomodation(String city) {
		List<AccomodationDTO> list = new ArrayList<>();
		List<Accomodation> accomodations = this.em
				.createNamedQuery(Accomodation.FIND_FOR_CITY, Accomodation.class)
				.setParameter("city", city).getResultList();
		for (Accomodation accomodation : accomodations) {
			list.add(accomodation.convertToDTO());
		}
		return list;
	}

	@Override
	public List<ActivityDTO> getActivity(String city) {
		List<ActivityDTO> list = new ArrayList<>();
		List<Activity> activities = this.em
				.createNamedQuery(Activity.FIND_FOR_CITY, Activity.class)
				.setParameter("city", city).getResultList();
		for (Activity activity : activities) {
			list.add(activity.convertToDTO());
		}
		return list;
	}

	@Override
	public List<TransportDTO> getFiltered(String origin, String destination) {
		List<TransportDTO> list = new ArrayList<>();
		List<Transport> transports = this.em
				.createNamedQuery(Transport.FIND_ORIGIN_DESTINATION, Transport.class)
				.setParameter("origin", origin).setParameter("destination", destination)
				.getResultList();
		for (Transport transport : transports) {
			list.add(transport.convertToDTO());
		}
		return list;
	}

	@Override
	public List<TransportDTO> getFromOrigin(String origin) {
		List<TransportDTO> list = new ArrayList<>();
		List<Transport> transports = this.em
				.createNamedQuery(Transport.FIND_ORIGIN, Transport.class)
				.setParameter("origin", origin).getResultList();
		for (Transport transport : transports) {
			list.add(transport.convertToDTO());
		}
		return list;
	}

	@Override
	public List<TransportDTO> getToDestination(String destination) {
		List<TransportDTO> list = new ArrayList<>();
		List<Transport> transports = this.em
				.createNamedQuery(Transport.FIND_DESTINATION, Transport.class)
				.setParameter("destination", destination).getResultList();
		for (Transport transport : transports) {
			list.add(transport.convertToDTO());
		}
		return list;
	}

	@Override
	public TransportDTO getTransport(Integer id) {
		Transport transport = this.em.find(Transport.class, id);
		return transport.convertToDTO();
	}

	@Override
	public AccomodationDTO getAccomodation(Integer id) {
		Accomodation accomodation = this.em.find(Accomodation.class, id);
		return accomodation.convertToDTO();
	}

	@Override
	public ActivityDTO getActivity(Integer id) {
		Activity activity = this.em.find(Activity.class, id);
		return activity.convertToDTO();
	}
}
